﻿using ActiveStoryTouch.DataModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Runtime.Serialization;
using System.IO;
using ActiveStoryTouch.DataModel.Helpers;
using System.Windows.Ink;
using ActiveStoryTouch.DataModel.Wpf;

namespace TestSuite.DataModel
{
    
    
    /// <summary>
    ///This is a test class for ProjectTest and is intended
    ///to contain all ProjectTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ProjectTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]
        public void ProjectConstructorTest()
        {
            Project target = new Project();
            Assert.AreEqual<int>(0, target.DefaultCanvasSettings.Alpha);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Red);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Green);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Blue);
            Assert.AreEqual<float>(1024, target.DefaultCanvasSettings.Width);
            Assert.AreEqual<float>(768, target.DefaultCanvasSettings.Height);
            Assert.IsNotNull(target.PageDictionary);
            Assert.AreEqual<int>(0, target.PageDictionary.Count);
            Assert.AreEqual<String>("Untitled Project", target.ProjectName);
        }

        /// <summary>
        ///A test for GetNextUniqueId
        ///</summary>
        [TestMethod]
        public void GetNextUniqueIdTest()
        {
            Project target = new Project();
            long expected = 1;
            long actual;
            actual = target.GetNextUniqueId();
            Assert.AreEqual(expected, actual);

            DataContractSerializer serializer = new DataContractSerializer(typeof(Project));
            FileStream fileStream = new FileStream(CommonTestConstants.ProjectUniqueIdTestFileName, FileMode.Create);
            serializer.WriteObject(fileStream, target);
            fileStream.Flush();
            fileStream.Close();

            fileStream = new FileStream(CommonTestConstants.ProjectUniqueIdTestFileName, FileMode.Open);
            Project projectFromFile = (Project) serializer.ReadObject(fileStream);
            fileStream.Close();

            expected = 2;
            actual = projectFromFile.GetNextUniqueId();
            Assert.AreEqual<long>(expected, actual);
        }

        /// <summary>
        ///A test for GetNextUniqueId
        ///</summary>
        [TestMethod]
        public void ProjectSerializationTest()
        {
            Project target = new Project();
            Assert.AreEqual<int>(0, target.DefaultCanvasSettings.Alpha);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Red);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Green);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Blue);
            Assert.AreEqual<float>(1024, target.DefaultCanvasSettings.Width);
            Assert.AreEqual<float>(768, target.DefaultCanvasSettings.Height);
            Assert.IsNotNull(target.PageDictionary);
            Assert.AreEqual<int>(0, target.PageDictionary.Count);
            Assert.AreEqual<String>("Untitled Project", target.ProjectName);

            target.DefaultCanvasSettings.Width = 1920;
            target.DefaultCanvasSettings.Height = 1080;
            target.DefaultCanvasSettings.Alpha = 128;
            target.DefaultCanvasSettings.Red = 128;
            target.DefaultCanvasSettings.Green = 128;
            target.DefaultCanvasSettings.Blue = 128;
            target.ProjectName = "Test Project";

            Page aPage = new Page { Name = CommonTestConstants.TestPageName, PageNumber = 1, UnqiueId = 1 };
            Assert.IsNotNull(aPage.PrototypeElementDictionary);
            Assert.IsNotNull(aPage.Strokes);
            PrototypeElement element = new PrototypeElement { ElementType = ElementTypesHelper.ElementTypeFromString(CommonTestConstants.TestElementTypeString), Top = 100.0, Left = 200.0, Width = 120.0, Height = 90.0, Content = "Test", ContentVisibile = false, HoverGlow = false, UniqueId = 1 };
            element.GestureTargetPageMap.Add(CommonTestConstants.TestGestureIdentifier, 1);
            PrototypeElement elementTwo = new PrototypeElement { ElementType = ElementTypes.ListBox, Top = 50.0, Left = 40.0, Width = 20.0, Height = 70.0, Content = "Test two!", ContentVisibile = true, HoverGlow = true, UniqueId = 2 };
            aPage.PrototypeElementDictionary.Add(element.UniqueId, element);
            aPage.PrototypeElementDictionary.Add(elementTwo.UniqueId, elementTwo);
            Assert.AreEqual<int>(2, aPage.PrototypeElementDictionary.Count);

            FileStream fs = new FileStream(Path.Combine(CommonTestConstants.RelativeTestProjectDirectory, "StrokeSerializationTest.isf"), FileMode.Open, FileAccess.Read, FileShare.Read);
            StrokeCollection strokeCollection = new StrokeCollection(fs);
            fs.Close();
            SerializableStrokeCollection serializableStrokeCollection = StrokeHelper.ConvertToSerializableStrokeCollection(strokeCollection);
            aPage.Strokes.AddRange(serializableStrokeCollection);
            Assert.AreNotEqual<int>(0, aPage.Strokes.Count);

            target.PageDictionary.Add(aPage.UnqiueId, aPage);

            target.SaveToFile(CommonTestConstants.ProjectSerializationTestFileName);

            DataContractSerializer serializer = new DataContractSerializer(typeof(Project));
            FileStream fileStream = new FileStream(CommonTestConstants.ProjectSerializationTestFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            Project targetFromFile = (Project) serializer.ReadObject(fileStream);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Alpha);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Red);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Green);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Blue);
            Assert.AreEqual<float>(1920, targetFromFile.DefaultCanvasSettings.Width);
            Assert.AreEqual<float>(1080, targetFromFile.DefaultCanvasSettings.Height);
            Assert.IsNotNull(targetFromFile.PageDictionary);
            Assert.AreEqual<int>(1, targetFromFile.PageDictionary.Count);
            Assert.AreEqual<String>("Test Project", targetFromFile.ProjectName);
            Assert.AreEqual<bool>(true, targetFromFile.PageDictionary.ContainsKey(1));
            Assert.AreEqual<String>(target.PageDictionary[1].Name, targetFromFile.PageDictionary[1].Name);
            Assert.AreEqual<long>(target.PageDictionary[1].PageNumber, targetFromFile.PageDictionary[1].PageNumber);
            Assert.AreEqual<long>(target.PageDictionary[1].UnqiueId, targetFromFile.PageDictionary[1].UnqiueId);
            Assert.AreEqual<long>(1, targetFromFile.GetNextUniqueId());
        }

        /// <summary>
        ///A test for GetNextUniqueId
        ///</summary>
        [TestMethod]
        public void ProjectDeserializationTest()
        {
            Project target = new Project();
            Assert.AreEqual<int>(0, target.DefaultCanvasSettings.Alpha);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Red);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Green);
            Assert.AreEqual<int>(255, target.DefaultCanvasSettings.Blue);
            Assert.AreEqual<float>(1024, target.DefaultCanvasSettings.Width);
            Assert.AreEqual<float>(768, target.DefaultCanvasSettings.Height);
            Assert.IsNotNull(target.PageDictionary);
            Assert.AreEqual<int>(0, target.PageDictionary.Count);
            Assert.AreEqual<String>("Untitled Project", target.ProjectName);

            target.DefaultCanvasSettings.Width = 1920;
            target.DefaultCanvasSettings.Height = 1080;
            target.DefaultCanvasSettings.Alpha = 128;
            target.DefaultCanvasSettings.Red = 128;
            target.DefaultCanvasSettings.Green = 128;
            target.DefaultCanvasSettings.Blue = 128;
            target.ProjectName = "Test Project";

            Page aPage = new Page { Name = CommonTestConstants.TestPageName, PageNumber = 1, UnqiueId = 1 };
            Assert.IsNotNull(aPage.PrototypeElementDictionary);
            Assert.IsNotNull(aPage.Strokes);
            PrototypeElement element = new PrototypeElement { ElementType = ElementTypesHelper.ElementTypeFromString(CommonTestConstants.TestElementTypeString), Top = 100.0, Left = 200.0, Width = 120.0, Height = 90.0, Content = "Test", ContentVisibile = false, HoverGlow = false, UniqueId = 1 };
            element.GestureTargetPageMap.Add(CommonTestConstants.TestGestureIdentifier, 1);
            PrototypeElement elementTwo = new PrototypeElement { ElementType = ElementTypes.ListBox, Top = 50.0, Left = 40.0, Width = 20.0, Height = 70.0, Content = "Test two!", ContentVisibile = true, HoverGlow = true, UniqueId = 2 };
            aPage.PrototypeElementDictionary.Add(element.UniqueId, element);
            aPage.PrototypeElementDictionary.Add(elementTwo.UniqueId, elementTwo);
            Assert.AreEqual<int>(2, aPage.PrototypeElementDictionary.Count);

            FileStream fs = new FileStream(Path.Combine(CommonTestConstants.RelativeTestProjectDirectory, "StrokeSerializationTest.isf"), FileMode.Open, FileAccess.Read, FileShare.Read);
            StrokeCollection strokeCollection = new StrokeCollection(fs);
            fs.Close();
            SerializableStrokeCollection serializableStrokeCollection = StrokeHelper.ConvertToSerializableStrokeCollection(strokeCollection);
            aPage.Strokes.AddRange(serializableStrokeCollection);
            Assert.AreNotEqual<int>(0, aPage.Strokes.Count);

            target.PageDictionary.Add(aPage.UnqiueId, aPage);

            DataContractSerializer serializer = new DataContractSerializer(typeof(Project));
            FileStream fileStream = new FileStream(CommonTestConstants.ProjectDeserializationTestFileName, FileMode.Create);
            serializer.WriteObject(fileStream, target);
            fileStream.Flush();
            fileStream.Close();

            Project targetFromFile = Project.LoadFromFile(CommonTestConstants.ProjectDeserializationTestFileName);
            
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Alpha);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Red);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Green);
            Assert.AreEqual<int>(128, targetFromFile.DefaultCanvasSettings.Blue);
            Assert.AreEqual<float>(1920, targetFromFile.DefaultCanvasSettings.Width);
            Assert.AreEqual<float>(1080, targetFromFile.DefaultCanvasSettings.Height);
            Assert.IsNotNull(targetFromFile.PageDictionary);
            Assert.AreEqual<int>(1, targetFromFile.PageDictionary.Count);
            Assert.AreEqual<String>("Test Project", targetFromFile.ProjectName);
            Assert.AreEqual<bool>(true, targetFromFile.PageDictionary.ContainsKey(1));
            Assert.AreEqual<String>(target.PageDictionary[1].Name, targetFromFile.PageDictionary[1].Name);
            Assert.AreEqual<long>(target.PageDictionary[1].PageNumber, targetFromFile.PageDictionary[1].PageNumber);
            Assert.AreEqual<long>(target.PageDictionary[1].UnqiueId, targetFromFile.PageDictionary[1].UnqiueId);
            Assert.AreEqual<long>(1, targetFromFile.GetNextUniqueId());
        }
    }
}
